package com.org.michaelbel.simple.slice;

import com.org.michaelbel.simple.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Button;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.NestedScrollView;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import org.michaelbel.bottomsheet.BottomSheet;
import org.michaelbel.bottomsheet.BottomSheetCallback;
import org.michaelbel.bottomsheet.BottomSheetClickListener;
import org.michaelbel.bottomsheet.util.ResTUtil;

/**
 * MainAbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice
        implements Component.ClickedListener, AbsButton.CheckedStateChangedListener {
    private int[] items =
            new int[]{
                    ResourceTable.String_share,
                    ResourceTable.String_upload,
                    ResourceTable.String_copy,
                    ResourceTable.String_print_this_page
            };

    private int[] gridItems =
            new int[]{
                    ResourceTable.String_share,
                    ResourceTable.String_upload,
                    ResourceTable.String_copy,
                    ResourceTable.String_print_this_page,
                    ResourceTable.String_message
            };
    private int[] icons =
            new int[]{
                    ResourceTable.Media_icon, ResourceTable.Media_icon, ResourceTable.Media_icon, ResourceTable.Media_icon
            };
    private int[] gridIcons =
            new int[]{
                    ResourceTable.Graphic_ic_share,
                    ResourceTable.Graphic_ic_upload,
                    ResourceTable.Graphic_ic_copy,
                    ResourceTable.Graphic_ic_print,
                    ResourceTable.Graphic_ic_message
            };
    private Checkbox cbTitle;
    Checkbox cbTitleMul;
    Checkbox cbIcons;
    Checkbox cbDividers;
    Checkbox cbFull;
    Checkbox cbCall;
    Checkbox cbGrid;
    Checkbox cbFab;
    Checkbox cbWindow;
    Checkbox cbCell;
    private boolean boolTitle;
    boolean boolTitleMul;
    boolean boolIcon;
    boolean boolDividers;
    boolean boolFull;
    boolean boolCall;
    boolean boolGrid;
    boolean boolFab;
    boolean boolWindow;
    boolean boolCell;

    BottomSheet.Builder bottomSheet;
    Slider slider;
    Slider sliderWindow;
    private int cellHeight = 48;
    private int bgAlphaValue = 20;
    private Text txtSlider;
    Text txtTopTitle;
    Text txtTitle;
    Text txtTitleMul;
    Text txtIcon;
    Text txtDivider;
    Text txtFull;
    Text txtCall;
    Text txtGrid;
    Text txtFab;
    Text txtWindow;
    Text txtCell;
    Text txtSliderWindow;

    DirectionalLayout rootl;
    DependentLayout dlTop;
    Button btnShow;

    boolean isWhiteStyle = true;
    private NestedScrollView rootScroll;
    private Image imgOne;
    private Image imgTow;
    private Image imgThree;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
        btnShow.setClickedListener(this);
        imgOne = (Image) findComponentById(ResourceTable.Id_img_top_one);
        imgTow = (Image) findComponentById(ResourceTable.Id_img_top_tow);
        imgThree = (Image) findComponentById(ResourceTable.Id_img_top_three);

        imgOne.setClickedListener(this);
        imgTow.setClickedListener(this);
        imgThree.setClickedListener(this);

        cbTitle = (Checkbox) findComponentById(ResourceTable.Id_cb_title);
        cbTitleMul = (Checkbox) findComponentById(ResourceTable.Id_cb_title_mul);
        cbIcons = (Checkbox) findComponentById(ResourceTable.Id_cb_icons);
        cbDividers = (Checkbox) findComponentById(ResourceTable.Id_cb_dividers);
        cbFull = (Checkbox) findComponentById(ResourceTable.Id_cb_full);
        cbCall = (Checkbox) findComponentById(ResourceTable.Id_cb_call);
        cbGrid = (Checkbox) findComponentById(ResourceTable.Id_cb_grid);
        cbFab = (Checkbox) findComponentById(ResourceTable.Id_cb_fab);
        cbWindow = (Checkbox) findComponentById(ResourceTable.Id_cb_window);
        cbCell = (Checkbox) findComponentById(ResourceTable.Id_cb_cell);

        cbTitle.setCheckedStateChangedListener(this);
        cbTitleMul.setCheckedStateChangedListener(this);
        cbIcons.setCheckedStateChangedListener(this);
        cbDividers.setCheckedStateChangedListener(this);
        cbFull.setCheckedStateChangedListener(this);
        cbCall.setCheckedStateChangedListener(this);
        cbGrid.setCheckedStateChangedListener(this);
        cbFab.setCheckedStateChangedListener(this);
        cbWindow.setCheckedStateChangedListener(this);
        cbCell.setCheckedStateChangedListener(this);
        initSlider();
    }

    private void initView() {
        rootl = (DirectionalLayout) findComponentById(ResourceTable.Id_rootl);
        dlTop = (DependentLayout) findComponentById(ResourceTable.Id_dl_top);
        rootScroll = (NestedScrollView) findComponentById(ResourceTable.Id_root_scroll);

        btnShow = (Button) findComponentById(ResourceTable.Id_btn_show);
        txtSlider = (Text) findComponentById(ResourceTable.Id_txt_slider);
        txtTopTitle = (Text) findComponentById(ResourceTable.Id_txt_top_title);
        txtTitle = (Text) findComponentById(ResourceTable.Id_txt_title);
        txtTitleMul = (Text) findComponentById(ResourceTable.Id_txt_title_mul);
        txtIcon = (Text) findComponentById(ResourceTable.Id_txt_icon);
        txtDivider = (Text) findComponentById(ResourceTable.Id_txt_divider);
        txtFull = (Text) findComponentById(ResourceTable.Id_txt_full);
        txtCall = (Text) findComponentById(ResourceTable.Id_txt_call);
        txtGrid = (Text) findComponentById(ResourceTable.Id_txt_grid);
        txtFab = (Text) findComponentById(ResourceTable.Id_txt_fab);
        txtWindow = (Text) findComponentById(ResourceTable.Id_txt_window);
        txtSliderWindow = (Text) findComponentById(ResourceTable.Id_txt_slider_window);
        txtCell = (Text) findComponentById(ResourceTable.Id_txt_cell);
        DirectionalLayout dlTitle = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_title);
        DirectionalLayout dlTitleMul = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_title_mul);
        DirectionalLayout dlIcons = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_icons);
        DirectionalLayout dlDividers = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_dividers);
        DirectionalLayout dlFull = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_full);
        DirectionalLayout dlCall = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_call);
        DirectionalLayout dlGrid = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_grid);
        DirectionalLayout dlFab = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_fab);
        DirectionalLayout dlWindow = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_window);
        DirectionalLayout dlCell = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_cell);

        dlTitle.setClickedListener(this);
        dlTitleMul.setClickedListener(this);
        dlIcons.setClickedListener(this);
        dlDividers.setClickedListener(this);
        dlFull.setClickedListener(this);
        dlCall.setClickedListener(this);
        dlGrid.setClickedListener(this);
        dlFab.setClickedListener(this);
        dlWindow.setClickedListener(this);
        dlCell.setClickedListener(this);
    }

    private void initSlider() {
        slider = (Slider) findComponentById(ResourceTable.Id_slider);

        slider.setMaxValue(70);
        slider.setMinValue(48);
        slider.setValueChangedListener(
                new Slider.ValueChangedListener() {
                    @Override
                    public void onProgressUpdated(Slider slider, int pos, boolean boo) {
                        cellHeight = pos;
                        txtSlider.setText(cellHeight + "");
                    }

                    @Override
                    public void onTouchStart(Slider slider) {
                    }

                    @Override
                    public void onTouchEnd(Slider slider) {
                    }
                });
        sliderWindow = (Slider) findComponentById(ResourceTable.Id_slider_window);
        sliderWindow.setMaxValue(100);
        sliderWindow.setMinValue(20);
        sliderWindow.setValueChangedListener(
                new Slider.ValueChangedListener() {
                    @Override
                    public void onProgressUpdated(Slider slider, int pos, boolean boo) {
                        bgAlphaValue = pos;
                        txtSliderWindow.setText(bgAlphaValue + "");
                    }

                    @Override
                    public void onTouchStart(Slider slider) {
                    }

                    @Override
                    public void onTouchEnd(Slider slider) {
                    }
                });
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btn_show:
                dialogMain();
                break;
            case ResourceTable.Id_img_top_one:
                toast("V1.0");
                break;
            case ResourceTable.Id_img_top_tow:
                toast("BottomSheet");
                break;
            case ResourceTable.Id_img_top_three:
                if (isWhiteStyle) {
                    sliceStyle();
                    isWhiteStyle = false;
                } else {
                    sliceStyle();
                    isWhiteStyle = true;
                }
                break;
            case ResourceTable.Id_dl_title:
                cbTitle.setChecked(!boolTitle);
                break;
            case ResourceTable.Id_dl_title_mul:
                cbTitleMul.setChecked(!boolTitleMul);
                break;
            case ResourceTable.Id_dl_icons:
                cbIcons.setChecked(!boolIcon);
                break;
            case ResourceTable.Id_dl_dividers:
                cbDividers.setChecked(!boolDividers);
                break;
            case ResourceTable.Id_dl_full:
                cbFull.setChecked(!boolFull);
                break;
            case ResourceTable.Id_dl_call:
                cbCall.setChecked(!boolCall);
                break;
            case ResourceTable.Id_dl_grid:
                cbGrid.setChecked(!boolGrid);
                break;
            case ResourceTable.Id_dl_fab:
                cbFab.setChecked(!boolFab);
                break;
            case ResourceTable.Id_dl_window:
                cbWindow.setChecked(!boolWindow);
                break;
            case ResourceTable.Id_dl_cell:
                cbCell.setChecked(!boolCell);
                break;
        }
    }

    @Override
    public void onCheckedChanged(AbsButton absButton, boolean boo) {
        switch (absButton.getId()) {
            case ResourceTable.Id_cb_title:
                boolTitle = boo;
                break;
            case ResourceTable.Id_cb_title_mul:
                boolTitleMul = boo;
                break;
            case ResourceTable.Id_cb_icons:
                boolIcon = boo;
                break;
            case ResourceTable.Id_cb_dividers:
                boolDividers = boo;
                break;
            case ResourceTable.Id_cb_full:
                boolFull = boo;
                break;
            case ResourceTable.Id_cb_call:
                boolCall = boo;
                break;
            case ResourceTable.Id_cb_grid:
                boolGrid = boo;
                break;
            case ResourceTable.Id_cb_fab:
                boolFab = boo;
                break;
            case ResourceTable.Id_cb_window:
                boolWindow = boo;
                break;
            case ResourceTable.Id_cb_cell:
                boolCell = boo;
                break;
        }
    }

    private void dialogMain() {
        bottomSheet = new BottomSheet.Builder(this);
        bottomSheet.setDarkTheme(isWhiteStyle);
        if (boolTitle) {
            bottomSheet.setTitle("This is Pretty!");
        }
        if (boolTitleMul) {
            bottomSheet.setTitleMultiline(true);
            bottomSheet.setTitle("That's a Bottom Sheet Dialog! Its Animated and more Attribute");
        }
        if (boolIcon) {
            bottomSheet.setItemsVectorIcons(
                    gridItems,
                    gridIcons,
                    new BottomSheetClickListener() {
                        @Override
                        public void onClickListener(int position, String str) {
                            toast(str);
                        }
                    });
        } else {
            bottomSheet.setItems(
                    gridItems,
                    new BottomSheetClickListener() {
                        @Override
                        public void onClickListener(int position, String str) {
                            toast(str);
                        }
                    });
        }
        if (boolDividers) {
            bottomSheet.setDividers(true);
        }
        if (boolFull) {
            bottomSheet.setFullWidth(false);
        }
        if (boolCall) {
            bottomSheet.setCallback(
                    new BottomSheetCallback() {
                        @Override
                        public void onShown() {
                            toast("onShown");
                        }
                        @Override
                        public void onDismissed() {
                            toast("onDismissed");
                        }
                    });
        }
        if (boolGrid) {
            bottomSheet.setContentType(BottomSheet.GRID);
        }
        if (boolWindow) {
            bottomSheet.setWindowDimming(bgAlphaValue);
        }
        if (boolCell) { // 只对list有效
            bottomSheet.setCellHeight(cellHeight);
        }
        bottomSheet.show();
    }

    private void sliceStyle() {
        rootl.setBackground(
                isWhiteStyle ? ResTUtil.buildDrawableByColor(0xFF424242) : ResTUtil.buildDrawableByColor(0xFFFFFFFF));
        dlTop.setBackground(
                isWhiteStyle ? ResTUtil.buildDrawableByColor(0xFF437771) : ResTUtil.buildDrawableByColor(0xFF0A9961));
        rootScroll.setBackground(
                isWhiteStyle ? ResTUtil.buildDrawableByColor(0xFF424242) : ResTUtil.buildDrawableByColor(0xFFFFFFFF));
        btnShow.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
        txtTitle.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
        txtTitleMul.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
        txtIcon.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
        txtDivider.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
        txtCall.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
        txtCell.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
        txtFull.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
        txtWindow.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
        txtGrid.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
        txtSlider.setTextColor(isWhiteStyle ? Color.WHITE : Color.BLACK);
    }

    @Override
    public void onActive() {
        super.onActive();
        toast("");
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void toast(String string) {
        new ToastDialog(this).setAlignment(LayoutAlignment.CENTER).setContentText(string).show();
    }
}
